﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;
 
namespace Twi.COMMON.WPF
{
    public class DataImporter 
    {
        public DataImporter(XElement xeEntity)
        {
            _xeEntity = xeEntity;
            DicConverter = new Dictionary<string, Func<string, string>>();
        }

        public void AddConverter(string name, Func<string, string> convert) 
        {
            DicConverter.Add(name, convert);
        }

        public Func<XElement, string, RequestResult> Persist;


        public Dictionary<string, Func<string, string>> DicConverter { get; set; }

        private XElement _xeEntity;
        public XElement XeEntity { get { return _xeEntity; } }


        public void DownloadDateImportTemplate() 
        {
            XElement[] xeColumns = this._xeEntity.Elements().ToArray();

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            try
            {
                Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Sheets worksheets = workbook.Worksheets;
                Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)worksheets.get_Item(1);

                Microsoft.Office.Interop.Excel.Range range;
                excel.Cells.Select();
                excel.Cells.RowHeight = 30;
                excel.Cells.NumberFormatLocal = "@";
                excel.Cells.Font.Size = 10;
                excel.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

                range = excel.Range[sheet.Cells[1, 1], sheet.Cells[1, xeColumns.Length]];
                range.Borders.LineStyle = 1;
                range.Font.Name = "宋体";
                range.Font.Size = 16;
                range.Font.Bold = true;
                range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                range.Select();

                range.EntireColumn.AutoFit();

                #region 设置标题

                for (var c = 1; c <= xeColumns.Length; c++)
                {
                    XAttribute xaDisplayName = xeColumns[c - 1].Attribute("DisplayName");
                    if (xaDisplayName == null)
                    {
                        continue;
                    }
                    sheet.Cells[1, c] = xaDisplayName.Value;
                    range = excel.Range[sheet.Cells[1, c], sheet.Cells[1, c]];
                    range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                    range.Font.Name = "宋体";
                    range.Font.Size = 13;
                    range.ColumnWidth = decimal.Parse(xeColumns[c - 1].Attribute("DisplayWidth").Value) / 12;

                }
                #endregion
                excel.SaveWorkspace(Type.Missing);
            }
            catch
            {
                
            }
            finally
            {
                IntPtr t = new IntPtr(excel.Hwnd);
                int k = 0;
                DllImportHelper.GetWindowThreadProcessId(t, out   k);
                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                p.Kill();
            }            
        }
           

        public void Import()
        {
            DataImportDialog did = new DataImportDialog(this);
            did.Loaded += new System.Windows.RoutedEventHandler(did_Loaded);
            did.ShowDialog();                 
        }

        void did_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as DataImportDialog).Process();
        }

         
    }
}
